function getMapFromCode(txt,place,click){

document.getElementById("mapdata").innerHTML = txt;
var codedata = txt.split("|");
var temp = codedata[1].split("X");
mapWidth = (temp[0]*1)+1;
codedata = codedata[0].split("");
var towrite = "<table style='border-spacing: 1px; background-color: A1A1AF;'><tr><th>&nbsp;</th>";
var count = 0;

for(var i = 0;i<=temp[0];i++){

towrite = towrite + "<th>" + i + "</th>";

}

towrite = towrite + "</tr>";

for(var i = 0;i<=temp[1];i++){

towrite = towrite + "<tr><th>" + i + "</th>";

for(var h = 0;h<=temp[0];h++){


towrite = towrite + "<td class='"+codedata[count]+"' height=32 width=32 id='";
if(click){ towrite = towrite + "click_"; }
towrite = towrite + count+"'";

if(click){

towrite = towrite + " onClick='locked(1);' onMouseMove='getData(\"click_"+count+"\")'";
}

towrite = towrite + "><img src='transparent.png'></td>";
count++;

}

towrite = towrite + "</tr>";

}

towrite = towrite + "</table>";

document.getElementById(place).innerHTML = towrite;

}





function getMap(){

var temp = document.getElementById("place").innerHTML;

if(temp == null || temp == ''){

document.getElementById("map").innerHTML = "No map selected.";
showSavedMaps(1);

}

else{

getMapFromCode(document.getElementById("mapcode").value,"map",1);
showSavedMaps(1);

}

}






function showSavedMaps(n){

if(n==1){

var towrite = "<table><tr><td>Sector: <input type='text' id='map_name'></tr></td><tr><td align='center'><select id='maps'>";
var temp;

for(var i = 0;i<4;i++){

temp = "PST_map_data_"+i;
towrite = towrite + "<option value='"+i+"'>";

if(getCookie(temp) == 0 || getCookie(temp) == ''){

towrite = towrite + "undefined";

}

else{

var nam = getCookie("PST_map_data_"+i);
nam = nam.split("++");
nam = nam[0];
towrite = towrite + nam;

}

towrite = towrite + "</option>";

}

towrite = towrite + "</select></td></tr><tr><td>";
towrite = towrite + "<input type='button' value='Save' onClick='saveMap();'>&nbsp;&nbsp;<input type='button' value='Load' onClick='loadMap();'>&nbsp;&nbsp;<input type='button' value='Delete' onClick='deleteMap();'></td></tr></table>";
document.getElementById("map").innerHTML = towrite + document.getElementById("map").innerHTML;

}

else{

for(var i = 0;i<4;i++){

var nam = getCookie("PST_map_data_"+i);
nam = nam.split("++");
nam = nam[0];
if(nam == 0){ nam = 'undefined'; }
document.getElementById("maps").options[i].innerHTML = nam;

}

}

}





function saveMap(){

var temp = true;
var onum = document.getElementById("maps").selectedIndex;
if(document.getElementById("maps").options[onum].innerHTML != 'undefined'){
temp = confirm("Are you sure you want to overwrite this saved sector?");
}
if(temp == true){
var mapname = document.getElementById("map_name").value;
var mapdata = mapname + "++" + document.getElementById("mapdata").innerHTML;
var buildingdata = '';
var temp;
var tcount = 0;
var id = '';

for(var i = 0;i<document.getElementsByTagName("td").length;i++){

if(document.getElementsByTagName("td")[i].getAttribute("onClick")!=null){

temp = document.getElementsByTagName("td")[i].innerHTML;

if(temp.search("transparent")==-1){

var name = document.getElementsByTagName("td")[i].childNodes[0].getAttribute("title");
var spandata = document.getElementsByTagName("td")[i].childNodes[1].innerHTML;

for(var h = 0;h<buildings.length;h++){

if(name == buildings[h][0]){ id=h; break; }

}

buildingdata = buildingdata + tcount + "=" + id + "=" + spandata + "|";

}

tcount = tcount + 1;

}

}

var selecte = document.getElementById("maps").selectedIndex;
setCookie("PST_map_data_"+selecte,mapdata);
setCookie("PST_map_buildings_"+selecte,buildingdata);
showSavedMaps(0);
}

}




function deleteMap(){

if(confirm("Are you sure you want to delete this map?")){
var selecte = document.getElementById("maps").selectedIndex;
setCookie("PST_map_data_"+selecte,"");
setCookie("PST_map_buildings_"+selecte,"");
showSavedMaps(0);
}

}




function loadMap(){

document.getElementById("menu").innerHTML = '';
var si = document.getElementById("maps").selectedIndex;
var mapdata = getCookie("PST_map_data_"+si);
var temp = mapdata.split("++");
var mapname = temp[0];
mapdata = temp[1];
var mapbuildings = getCookie("PST_map_buildings_"+si);

document.getElementById("mapcode").value = mapdata;
getMapFromCode(document.getElementById("mapcode").value,"place",0);
getMap();
document.getElementById("map_name").value = mapname;
var psplit = mapbuildings.split("|");
document.getElementById("maps").selectedIndex = si;

for(var i = 0;i<psplit.length;i++){

psplit[i] = psplit[i].split("=");

}

for(var h = 0;h<(psplit.length-1);h++){

var IP = getIP();
var tsrc = buildings[psplit[h][1]][1][1];
var ttitle = buildings[psplit[h][1]][0];
ttemp = "click_"+psplit[h][0].replace(" ","");
document.getElementById(ttemp).innerHTML = "<img src='"+IP+"foregrounds/"+tsrc+"' height='32' width='32' title='"+ttitle+"'><span style='display:none;'>"+psplit[h][2]+"</span>";

}

}

function importSector(){

document.getElementById("menu").innerHTML = '';
var temp = prompt("Paste your sector code here.","");
if(temp!=''){

temp = temp.replace(/ /g,"");
var temp = temp.split("<>");
var mapdata = temp[0];
var ttemp = mapdata.split("++");
var mapname = ttemp[0];
mapdata = ttemp[1];
var mapbuildings = temp[1];
importPL(1,temp[2]);

document.getElementById("mapcode").value = mapdata;
getMapFromCode(document.getElementById("mapcode").value,"place",0);
getMap();
document.getElementById("map_name").value = mapname;
var psplit = mapbuildings.split("|");

for(var i = 0;i<psplit.length;i++){

psplit[i] = psplit[i].split("=");

}

for(var h = 0;h<(psplit.length-1);h++){

var IP = getIP();
var tsrc = buildings[psplit[h][1]][1][1];
var ttitle = buildings[psplit[h][1]][0];
ttemp = "click_"+psplit[h][0].replace(" ","");
document.getElementById(ttemp).innerHTML = "<img src='"+IP+"foregrounds/"+tsrc+"' height='32' width='32' title='"+ttitle+"'><span style='display:none;'>"+psplit[h][2]+"</span>";

}
}

}

function exportSector(){

var mapname = document.getElementById("map_name").value;
var mapdata = mapname + "++" + document.getElementById("mapdata").innerHTML;
var buildingdata = '';
var temp;
var tcount = 0;
var id = '';

for(var i = 0;i<document.getElementsByTagName("td").length;i++){

if(document.getElementsByTagName("td")[i].getAttribute("onClick")!=null){

temp = document.getElementsByTagName("td")[i].innerHTML;

if(temp.search("transparent")==-1){

var name = document.getElementsByTagName("td")[i].childNodes[0].getAttribute("title");
var spandata = document.getElementsByTagName("td")[i].childNodes[1].innerHTML;

for(var h = 0;h<buildings.length;h++){

if(name == buildings[h][0]){ id=h; break; }

}

buildingdata = buildingdata + tcount + "=" + id + "=" + spandata + "|";

}

tcount = tcount + 1;

}

}

var PL = document.getElementById("temppl").innerHTML;
var together = mapdata + "<>" + buildingdata + "<>" + PL;
together = together.replace(" ","");
var n = prompt("Save this code in a text document or on the official Pardus Sector Tool website.",together);

}